﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SKYPE4COMLib;
using SoundServant;
using System.Windows.Threading;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for SkypeControl.xaml
    /// </summary>
    public partial class SharingControl : UserControl
    {
        private Sharing sharing;
        private delegate void VoidInvokeHandler();

        public SharingControl(Sharing _sharing)
        {
            InitializeComponent();
            sharing = _sharing;

            AutoCheckBox.Checked += new RoutedEventHandler(AutoCheckBox_Checked);
            AutoCheckBox.Unchecked += new RoutedEventHandler(AutoCheckBox_Unchecked);
            sharing.Detached += new Sharing.VoidEventHandler(sharing_Detached);


            sharing.AttachmentStatus += new Sharing.AttachmentStatusEventHandler(sharing_AttachmentStatus);
            sharing.CallsUpdated += new Sharing.VoidEventHandler(sharing_CallsUpdated);

            sharing.CallsStarted += new Sharing.VoidEventHandler(sharing_CallsStarted);
            sharing.CallsFinished += new Sharing.VoidEventHandler(sharing_CallsFinished);

            sharing.ListenerAdded += new Sharing.ListenerEventHandler(sharing_ListenerAdded);
            sharing.ListenerDeleted += new Sharing.ListenerEventHandler(sharing_ListenerDeleted);
            sharing.ListenersUpdated += new Sharing.VoidEventHandler(sharing_ListenersUpdated);

            DrawListeners();
        }

        void sharing_CallsFinished()
        {
            SS.OneSecondTimer.Tick -= OneSecondTimer_Tick;
            StatusLabel.Content = "Call Finished";
            CallStatusLabel.Content = "Call Finished";
            ConnectDisconnectButtonLabel.Content = "Connect";
            InformationLabel.Content = "Click [Connect] to make a call";
        }

        void OneSecondTimer_Tick(object sender, EventArgs e)
        {
            CallTimerLabel.Content = SS.ParseTimeSpan(sharing.CallTime);
        }

        void sharing_CallsStarted()
        {
            SS.OneSecondTimer.Tick += new EventHandler(OneSecondTimer_Tick);
            if (ConnectDisconnectButton.IsEnabled == false)
                ConnectDisconnectButton.IsEnabled = true;
            ConnectDisconnectButtonLabel.Content = "Disconnect";
            InformationLabel.Content = "Click [Disconnect] to drop all listeners";
        }

        void sharing_Detached()
        {
            StatusLabel.Content = "Detatched";
            InformationLabel.Content = "Click [Reset] to connect Skype";
        }

        void sharing_ListenersUpdated()
        {
            DrawListeners();
        }

        void sharing_ListenerDeleted(Listener listener)
        {
            foreach (ListenerControl listenerControl in ListenersStackPanel.Children)
            {
                if (listenerControl.Listener.Id == listener.Id)
                {
                    ListenersStackPanel.Children.Remove(listenerControl);
                    break;
                }
            }
        }

        void sharing_ListenerAdded(Listener listener)
        {
            ListenersStackPanel.Children.Add(new ListenerControl(listener));
        }

        void sharing_AttachmentStatus(TAttachmentStatus attachmentStatus)
        {
            switch (attachmentStatus)
            {
                case TAttachmentStatus.apiAttachSuccess:
                    StatusLabel.Content = "Connected";
                    InformationLabel.Content = "Click [Connect] to make a call";
                    SkypeUserStatusLabel.Content = sharing.CurrentUserHandle + " | " + sharing.BalanceString;
                    break;
                case TAttachmentStatus.apiAttachPendingAuthorization:
                    StatusLabel.Content = "Attach Pending Authorization";
                    InformationLabel.Content = "Please wait while Sound Servant attaches to Skype";
                    break;
                case TAttachmentStatus.apiAttachNotAvailable:
                    StatusLabel.Content = "Attach Unavailable";
                    InformationLabel.Content = "Please wait while Sound Servant connects to Skype";
                    break;
                case TAttachmentStatus.apiAttachAvailable:
                    StatusLabel.Content = "Attaching";
                    InformationLabel.Content = "Please wait while Sound Servant connects to Skype";
                    break;
                case TAttachmentStatus.apiAttachRefused:
                    StatusLabel.Content = "Attach Failed";
                    InformationLabel.Content = "Please create a snapshot of skype where Sound Servant is Authorized";
                    break;
                default:
                    StatusLabel.Content = "Attaching?";
                    InformationLabel.Content = "Please wait while Sound Servant connects to Skype";
                    break;

            }

        }

        void sharing_CallsUpdated()
        {
            if (sharing.Calls.Count > 0)
            {
                StatusLabel.Content = "Call In Progress | " + sharing.Calls.Count + " listener(s)";
                CallStatusLabel.Content = "Call In Progress | " + sharing.Calls.Count + " listener(s)";
            }
        }


        void sharing_Attaching()
        {
            StatusLabel.Content = "Attaching to Skype";
            InformationLabel.Content = "Please wait while Sound Servant connects to Skype";
        }

        void sharing_InitializeTimedOut()
        {
            StatusLabel.Content = "Skype Failed to Load";
            InformationLabel.Content = "Skype has failed to load, please inform your Sound Servant of this error";
        }

        private void DrawListeners()
        {
            foreach (Listener listener in sharing.Listeners)
            {
                ListenersStackPanel.Children.Add(new ListenerControl(listener));
            }
        }
        private void PlaceCallButton_Click(object sender, RoutedEventArgs e)
        {
            sharing.ConnectDisconnect();
        }

        private void InitializeAttachButton_Click(object sender, RoutedEventArgs e)
        {
            if (SSMessageBox.Show("Reset Skype?", "Are you sure you want to Reset Skype? This will end any calls currently in progress.", SSMessageBoxType.YesNo) == SSMessageBoxResult.Yes)
            {
                sharing.Initialize();
            }
        }

        private void AutoCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            sharing.AutoDial = true;
        }

        private void AutoCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            sharing.AutoDial = false;
        }

    }
}